
define(function (require, exports, module) {
    require("Firebird.App");
    require("Firebird.Controller");
    require("Format");

    var controller = Firebird.create("Firebird.Controller", {
        controller: "{contextPath}/etl/etldatasourcecfg"
    });

    Firebird.define("etldatasourcecfg.SearchApp", {
        extend: "Firebird.SearchApp",
        controller: controller,
        setContext: function () {
            var app = this,context = this.context;

            var formhelper = this.context.J_form;
            this.checkComponentsPromise(formhelper, {}, function () {
            var J_grid = context.J_grid = Firebird.create("Firebird.ui.Grid", {
                renderTo: $("#J_grid"),
                formhelper: formhelper,
                controller: controller,
                otherHeight: 160,
                rownumWidth: 35,
                shrinkToFit: true,
                jsonReader: {id: "id"},
                colModel: [
                    {name: "id", hidden: true, width: 1, frozen: true},
                    {name: "connName", label: "连接名称",sortable: false, width: 30, align: "center",
                        formatter: function (val, me, row) {
                            return require("tmpl").compile("<a href='javascript:;' data-id='{{id}}'>{{connName}}</a>")(row);
                        }
                    },
                    {name: "serverAddr", label: "服务器地址",sortable: false, width: 30, align: "center"},
                    {name: "type", label: "数据库类型",sortable: false, width: 30, align: "center"},
                    {name: "name", label: "数据库名称",sortable: false, width: 50, align: "center"},
                    {name: "port", label: "端口号",sortable: false, width: 20, align: "center"},
                    {name: "user", label: "连接账号",sortable: false, width: 20, align: "center"}
                ]
            });
                //J_grid.onAutosize = function () {
                //    J_grid.setGridHeight($(window).height() - 210);
                //}
            });
        },
        listener: {
            "click::#J_grid>>a": "view"
        },

        setActionIds: function (e) {
            this.actionIds = typeof $(e.target).data("id") != "undefined" ? [$(e.target).data("id")] : this.context.J_grid.getCheckedRowIds();
            return this;
        }
    });

    Firebird.define("etldatasourcecfg.SaveApp", {
        extend: "Firebird.SaveApp",
        controller: controller,
        setContext: function () {
            var app = this,context = this.context;
        },
        /*afterLaunch: function () {
            this.superclass.afterLaunch.apply(this, []);
            this.context.dialog.title("新增数据源信息");
            this.context.dialog.width(1000);
            this.context.dialog.height(_top.$(_top). height() - 350);
        },*/
        afterLaunch: function () {
            var me = this;
            var context = this.context;

            this.superclass.afterLaunch.apply(this, []);
            this.context.dialog.title("新增数据源信息");
            this.context.dialog.width(1000);
            this.context.dialog.height(_top.$(_top). height() - 350);

            context.data = firebird.data.get(firebird.url.getParameter("_FBID_") || window.name) || {};
            context.dialog = _top.$.dialog && _top.$.dialog.get(window) || require("dialog");
            context.dialog && context.dialog.button && context.dialog.button(me.setButtons([
                {
                    value: "确 定",
                    autofocus: true,
                    callback: function () {
                        me.bindSaveValidater();
                        return false;
                    }
                },
                {
                    value: "取 消"
                },
                {
                    value: "测 试",
                    autofocus: true,
                    callback: function () {
                        me.bindTestValidater();
                        return false;
                    }
                }
            ]));
            this.hideProgress();
        },
        bindSaveValidater: function () {
            var me = this, context = this.context;
            context.J_form.instance.isValid(function (result) {
                if (result == true) {
                    var tip = Firebird.ui.Dialog.tips("正在处理新增操作...", 100000, {lock: true});
                    me.controller.insert(context.J_form.getValue(), {}, function (data) {
                        tip.remove();
                        //firebird.info("Firebird.App::insert complete ", data);
                        if (data.success === true) {
                            me.context.dialog.close(data);
                        } else {
                            new Firebird.lang.AjaxException({
                                message: data.data && data.data.replace(/\;/, ";\n")
                            });
                        }
                    });
                }
            });
        },
        bindTestValidater: function () {
            var me = this, context = this.context;
            context.J_form.instance.isValid(function (result) {
                if (result == true) {
                    var tip = Firebird.ui.Dialog.tips("正在处理测试操作...", 100000, {lock: true});
                    var param = context.J_form.getValue();
                    console.log(param);
                    Firebird.create("Firebird.ajax.Post", {
                        url: controller.getAction("/connTest").url,
                        data: {
                                connName: param.connName,
                                type: param.type,
                                name: param.name,
                                serverAddr: param.serverAddr,
                                port: param.port,
                                user: param.user,
                                password: param.password
                                },
                        success: function (data) {
                            tip.remove();
                            if (data.success) {
                                Firebird.ui.Dialog.alert("连接成功");
                            } else {
                                Firebird.ui.Dialog.error("连接失败！");
                            }
                        }
                    });
                    /*me.controller.insert(context.J_form.getValue(), {}, function (data) {
                        tip.remove();
                        //firebird.info("Firebird.App::insert complete ", data);
                        if (data.success === true) {
                            me.context.dialog.close(data);
                        } else {
                            new Firebird.lang.AjaxException({
                                message: data.data && data.data.replace(/\;/, ";\n")
                            });
                        }
                    });*/
                }
            });
        },
        handlers: {

        },
        setButtons: function (buttons) {
            buttons[0].value = "保存";
            buttons[1].value = "取消";
            return buttons;
        }
    });



    Firebird.define("etldatasourcecfg.EditApp", {
        extend: "Firebird.EditApp",
        controller: controller,
        setContext: function () {
            var app = this,context = this.context;
        },
        afterLaunch: function () {
            this.superclass.afterLaunch.apply(this, []);
            this.context.dialog.title("修改数据源信息");
            this.context.dialog.width(1000);
            this.context.dialog.height(_top.$(_top). height() - 350);
        },
        setButtons: function (buttons) {
            buttons[0].value = "保存";
            buttons[1].value = "取消";
            return buttons;
        },
        setValue: function (callback) {
            var me = this;
            var context = this.context;
            me.controller.select(context.data.app.getActionIds()[0], {}, function (json) {
                me.checkComponentsPromise(context.J_form, json.data, function () {
                    if (me.beforeSetValue.apply(me, [context.J_form, json.data]) == true) {
                        context.J_form.setValue(json.data);

                    }
                    me.afterSetValue();
                });
            });
        }
    });



    Firebird.define("etldatasourcecfg.ViewApp", {
        extend: "Firebird.ViewApp",
        controller: controller,
        setContext: function () {
            var app = this,context = this.context;
        },
        afterLaunch: function () {
            this.superclass.afterLaunch.apply(this, []);
            this.context.dialog.title("查看数据源信息");
            this.context.dialog.width(1000);
            this.context.dialog.height(_top.$(_top). height() - 350);
        },
        setValue: function (callback) {
            var me = this;
            var context = this.context;
            me.controller.select(context.data.app.getActionIds()[0], {}, function (json) {
                me.checkComponentsPromise(context.J_form, json.data, function () {
                    if (me.beforeSetValue.apply(me, [context.J_form, json.data]) == true) {
                        context.J_form.setValue(json.data);

                    }
                    me.afterSetValue();
                });
            });
        }
    });
});
